package com.ly.blog2Common.core.http;

import org.springframework.beans.BeansException;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Description: 错误信息存储体
 * @Authur: lyMaster
 * @Date: 2018/12/30 09:21
 */

public class ErrConfigInit {
    public static Map<Integer,String> errMap ;

    private static void processProperties(Properties props) throws BeansException {
        errMap = new HashMap();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            try {
                // PropertiesLoaderUtils的默认编码是ISO-8859-1,在这里转码一下
                errMap.put(Integer.parseInt(keyStr), new String(props.getProperty(keyStr).getBytes("ISO-8859-1"), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void loadAllProperties(String propertyFileName) {
        try {
            Properties properties = PropertiesLoaderUtils.loadAllProperties(propertyFileName);
            processProperties(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getErrMsg(Integer errCode) {
        return errMap.get(errCode).toString();
    }

    public static Map<Integer, String> getErrMap() {
        return errMap;
    }

}
